package larrys.pomodoro.domain.repository;

import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

import junit.framework.Assert;
import larrys.pomodoro.domain.entity.Activity;
import larrys.pomodoro.domain.entity.PomodoroUser;
import larrys.pomodoro.domain.entity.Role;
import larrys.pomodoro.domain.entity.Round;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

public class RepositoryTestBase extends
		AbstractTransactionalJUnit4SpringContextTests {

	@Autowired
	protected JdbcTemplate jdbcTemplate;

	private AtomicLong mockSuffix = new AtomicLong(0);

	protected PomodoroUser addMockUserAndReturn() {
		String email = "mockUser" + mockSuffix.getAndIncrement() + "@mock.com";
		String username = "mockuser";
		String password = "123";
		boolean male = true;
		boolean expired = false;
		boolean locked = false;
		boolean credentialsExpired = false;
		boolean enabled = true;

		String roleName = "ROLE_USER_MOCK";

		simpleJdbcTemplate.update(
				"INSERT INTO user VALUES(null,?,?,?,?,?,?,?,?,0)", email,
				username, password, male, expired, locked, credentialsExpired,
				enabled);

		simpleJdbcTemplate.update("INSERT INTO role VALUES(null,?)", roleName);

		long userId = simpleJdbcTemplate.queryForLong(
				"SELECT id FROM user WHERE email = ?", email);
		Assert.assertTrue(userId > 0);

		long roleId = simpleJdbcTemplate.queryForLong(
				"SELECT id FROM role WHERE role_name = ?", roleName);
		Assert.assertTrue(roleId > 0);

		simpleJdbcTemplate.update("INSERT INTO user_role values(?,?)", userId,
				roleId);

		PomodoroUser user = new PomodoroUser(userId, email);
		user.setUsername(username);
		user.setPassword(password);
		user.setMale(male);
		user.setExpired(expired);
		user.setLocked(locked);
		user.setCredentialsExpired(credentialsExpired);
		user.setEnabled(enabled);
		user.addRole(new Role(roleId, roleName));

		return user;
	}

	protected Activity addMockActivityAndReturn(long userId, boolean finished,
			boolean permanent) {
		String name = "mockActivity" + mockSuffix.getAndIncrement();
		int estimation = 3;
		String description = "mockDesc";
		String comment = "mockActivityComment";
		Date createDate = new Date();
		int priority = 1;

		simpleJdbcTemplate.update(
				"INSERT INTO activity VALUES(null,?,?,?,?,?,?,0,?,?,?)", name,
				estimation, description, comment, finished, permanent, userId,
				createDate, priority);
		long activityId = simpleJdbcTemplate.queryForLong(
				"SELECT id FROM activity WHERE name = ?", name);
		Assert.assertTrue(activityId > 0);

		Activity activity = new Activity(activityId, name, userId, permanent,
				finished);
		activity.setEstimation(estimation);
		activity.setDescription(description);
		activity.setComment(comment);
		activity.setPlanDate(createDate);
		activity.setPriority(priority);
		return activity;
	}

	protected Round addMockRoundAndReturn(long userId, long activityId,
			Date startTime, Date endTime, boolean interrupted, boolean finished) {
		int duration = 60 * 25;
		String comment = "mockRoundComment" + mockSuffix.getAndIncrement();
		simpleJdbcTemplate.update(
				"INSERT INTO round VALUES(null,?,?,?,?,?,?,0,?,?)", duration,
				startTime, endTime, interrupted, finished, comment, userId,
				activityId);
		long roundId = simpleJdbcTemplate.queryForLong(
				"SELECT id FROM round WHERE comment = ?", comment);
		Round round = new Round(roundId, userId, activityId, finished);
		Assert.assertTrue(roundId > 0);
		round.setComment(comment);
		round.setDuration(duration);
		round.setStartTime(startTime);
		round.setEndTime(endTime);
		round.setInterrupted(interrupted);
		return round;
	}
}
